<?php
require_once("../apps/resources/commons.php");
require_once("../apps/data/User.php");
require_once("../apps/data/Item.php");
require_once("HTTP.php");
//Get the feedkey (unique for each user)
$l_feedKey=$_REQUEST["fk"];

if(isset($l_feedKey)){
	$l_user=array_shift(User::getFromCustomCriteria("WHERE feedKey=:feedKey",array("feedKey" => $l_feedKey)));
	if(isset($l_user)){
		$l_items=$l_user->getOpenAllocatedActions();
		if(count($l_items)!=0){	
			$l_updatedFlagSql="SELECT min(created) as updated FROM item i where ItemType_id=" . Item::ACTION_TYPE . " and ItemStatus_id=" . Item::ACTION_STATUS_OPENED .  " and AllocatedUser_id='" . $l_user->getId() . "';";
			
			//Get the last modification date
			try {
				$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
				foreach ($l_dbh->query($l_updatedFlagSql) as $l_row) {
					$l_updatedFlag=date("c", strtotime($l_row["updated"]));
				}
				$l_dbh = null;
			} catch (PDOException $e) {
				print "Error ! : " . $e->getMessage() . "<br/>";
				die();
			}
		}else{
			$l_updatedFlag=date("c", time());
		}
		
		header('Content-type: text/xml; charset=iso-8859-1');
		echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>My pending actions</title>
	<link href="http://<?=$_SERVER["SERVER_NAME"] ?><?=ROOT_PATH ?>/rss/dashboard_rss.php?fk=<?=$l_feedKey ?>" />
	<updated><?=$l_updatedFlag ?></updated>
	<author>
		<name><?=$l_user->getFullName() ?></name>
	</author>
	<id>urn:uuid:<?=$l_feedKey ?></id>
	<?php
		foreach($l_items as $l_item){
	?>
	<entry>
		<?php
		if($l_item->getOverdue()){
			$l_itemTitle="OVERDUE: " .$l_item->getTitle();
		}else{
			$l_itemTitle=$l_item->getTitle();
		}
		?>
		<title><?=$l_itemTitle ?></title>
		<link href="https://<?=$_SERVER["SERVER_NAME"] ?><?=ROOT_PATH ?>/apps/user/dashboard/display_action.php?id=<?=base64_encode($l_item->getId()) ?>" />
		<id>urn:uuid:<?=base64_encode($l_item->getId()) ?></id>
		<updated><?=date("c", strtotime($l_item->getAllocated())) ?></updated>
		<summary type="xhtml">
			<div xmlns="http://www.w3.org/1999/xhtml">
			<p><b>Target date: </b><?=$l_item->getTargetDate() ?></p>
			</div>
		</summary>
	</entry>
	<?php
		}
	?>
</feed>
<?php
	}else{
		HTTP::redirect("http://www.microsoft.com");
	}
}else{
	HTTP::redirect("http://www.microsoft.com");
}
?>